<template>
  <div>
    <div class="container">
      <div class="wrapper">
        <el-form :model="registerInfo" :rules="rules" @submit.native.prevent="submitForm" ref="ruleForm">
          <h1>{{ loginFlag ? '登录' : '注册' }}</h1>
          <el-form-item prop="username">
            <el-input
              v-model="registerInfo.username"
              placeholder="用户名"
              prefix-icon="el-icon-user"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="registerInfo.password"
              type="password"
              placeholder="密码"
              prefix-icon="el-icon-lock"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item v-if="!loginFlag" prop="email">
            <el-input
              v-model="registerInfo.email"
              type="email"
              placeholder="邮箱"
              prefix-icon="el-icon-message"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit" class="btn">
              {{ loginFlag ? '登录' : '注册' }}
            </el-button>
          </el-form-item>
          <div class="signup-link">
            <p>
              {{ loginFlag ? '没有账号？' : '已有账号？' }}
              <a href="#" @click.prevent="changeFlag">{{ loginFlag ? '创建一个' : '返回登录' }}</a>
            </p>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { validUsername, validPassword, validEmail } from '@/utils/validate'
export default {
  data() {
    // 校验用户名
    const validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else {
        if (!validUsername(value)) {
          callback(new Error('用户名必须为 数字+字母 6-12位'))
        } else {
          callback()
        }
      }
    }
    // 校验密码
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (!validPassword(value)) {
          callback(new Error('密码8-16个字符，至少1个字母，1个数字'))
        } else {
          callback()
        }
      }
    }
    // 校验邮箱
    const validateEmail = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入邮箱'))
      } else {
        if (!validEmail(value)) {
          callback(new Error('邮箱格式不正确'))
        } else {
          callback()
        }
      }
    }
    return {
      loginFlag: true,
      registerInfo: {
        username: 'lanxi66',
        password: 'lx123456',
        email: ''
      },
      loginInfo: {
        username: 'lanxi66',
        password: 'lx123456'
      },
      rules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        email: [{ required: true, trigger: 'blur', validator: validateEmail }]
      }
    }
  },
  methods: {
    ...mapActions('user', ['userRegister']),
    // 切换登录注册
    changeFlag() {
      this.loginFlag = !this.loginFlag
    },
    // 注册和登录逻辑
    async submitForm() {
      // 登录
      if (this.loginFlag) {
        try {
          this.loginInfo.username = this.registerInfo.username
          this.loginInfo.password = this.registerInfo.password
          await this.$store.dispatch('user/userLogin', this.loginInfo)
          this.$message.success({
            message: '登录成功',
            offset: 80
          })
          // 跳转到首页
          this.$router.push('/')
          this.$store.dispatch('user/userInfo')
        } catch (error) {
          this.$message.error(error.message)
        }
      } else {
        // 注册
        try {
          await this.$store.dispatch('user/userRegister', this.registerInfo)
          this.$message.success({
            message: '注册成功',
            offset: 80
          })
          // 切换到登录页面
          this.changeFlag()
        } catch (error) {
          this.$message.error(error.message)
        }
      }
    },
    // 获取用户信息
    async getUserInfo() {}
  }
}
</script>

<style lang="scss" scoped>
* {
  font-family: 'Poppins', sans-serif;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

@media (max-width: 360px) {
  .wrapper {
    width: 100%;
    height: 100vh;
    border: none;
    border-radius: 0px;
  }

  .wrapper .input-box {
    width: 290px;
  }
  #img {
    z-index: -90;
  }
}

.container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('@/assets/img/login-bg.jpg');
  background-size: cover;
  background-position: center;
}

.wrapper {
  position: relative;
  width: 400px;
  height: 450px;
  background: rgba(237, 238, 254, 0.1);
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(3px);
  h1 {
    font-size: 25px;
    color: #fff;
    text-align: center;
  }
  .el-form-item {
    width: 310px;
    margin: 30px 0;
    .el-input {
      width: 100%;
      background: transparent;
      input {
        background: transparent;
        color: #fff;
        border: none;
        border-bottom: 2px solid #fff;
        border-radius: 0;
        padding-left: 5px;
        &::placeholder {
          color: #fff;
        }
      }
      .el-input__prefix {
        color: #fff;
      }
    }
  }
  .btn {
    width: 100%;
    height: 40px;
    background: #fff;
    border: none;
    border-radius: 40px;
    cursor: pointer;
    font-size: 18px;
    font-weight: 500;
    color: #000;
    margin-top: 10px;
    &:hover {
      background: #197599;
    }
  }
  .signup-link {
    font-size: 16px;
    color: #fff;
    text-align: end;
    margin: 25px 0 10px;
    a {
      color: skyblue;
      text-decoration: none;
      font-weight: 600;
      &:hover {
        text-decoration: underline;
      }
    }
  }
}
</style>
